/**
 * tabBar页面路径列表 (用于链接跳转时判断)
 * tabBarLinks为常量, 无需修改
 */
const tabBarLinks = [
  'pages/index/index',
  'pages/article/index',
  'pages/active/index',
  'pages/category/index',
  'pages/user/index',
  "pages/palm/index"//sg添加
];

// 站点配置文件
import siteinfo from './siteinfo.js';

// 工具类
import util from './utils/util.js';


// 直播 S
var platform = null;

"undefined" != typeof wx && (platform = "wx"), "undefined" != typeof my && (platform = "my");

var modules = [{
        name: "const",
        file: "./core/const.js"
    }, {
      name: "bus",
      file: "./core/bus.js"
    }, {
        name: "page",
        file: "./core/page.js"
    }, {
        name: "request",
        file: "./core/request.js"
    }, {
        name: "core",
        file: "./core/core.js"
    }, {
        name: "api",
        file: "./core/api.js"
    }, {
        name: "getUser",
        file: "./core/getUser.js"
    }, {
        name: "setUser",
        file: "./core/setUser.js"
    }, {
        name: "login",
        file: "./core/login.js"
    }],
    args = {
        _version: "2.8.9",
        platform: platform,
        query: null,
        is_login: !1,
        login_complete: !1,
        windowWidth: wx.getSystemInfoSync().windowWidth,
        windowHeight: wx.getSystemInfoSync().windowHeight,
        customerId: wx.getStorageSync('customerId') || ''
    };

for (var i in modules) args[modules[i].name] = require("" + modules[i].file);
// 直播 S

App({
  args,
  /**
   * 全局变量
   */
  globalData: {
    user_id: null,
    //变美模块 
    tempPhoto:"",
    // 描点缩放比
    pie:"",
    //变美魔镜 前后图片
    imgArr:[],
    // 美学三维 参数
    mxswArg:{},
    // 脸部信息
    dotInfo:[],
    // 当前解决方案
    currenSolution:""
  },

  // api地址
  api_root: siteinfo.siteroot + 'index.php?s=/api/',

  // 变美模块api 列表
  pfjc:siteinfo.bianmei+ "/ToolCenter/skin/skinanalyze_pro", //皮肤检测图片 上传
  zxsz_cs_list:siteinfo.bianmei+ "/ToolCenter/lipColor/lips",  //在线试妆唇色列表
  zxsz:siteinfo.bianmei+"/ToolCenter/lipColor/change", // 选择唇色后 获取的图片
  rxbh_nl:siteinfo.bianmei+"/ToolCenter/ageChange/change", //人像变化 选择年龄
  rxbh_xb:siteinfo.bianmei+"/ToolCenter/sexChange/change", //人像变化 选择性别
  rxbh_dm:siteinfo.bianmei+"/ToolCenter/cartoonChange/change", //人像变化 选择动漫
  bmmj_mnfg:siteinfo.bianmei+"/ToolCenter/styleChange/change", //变美魔镜 模拟风格
  bmmj_znmxpd:siteinfo.bianmei+"/ToolCenter/facePlus/thousandlandmark", // 变美魔镜 智能美学判断
  bmmj_mnzx:siteinfo.bianmei+"/ToolCenter/simulationChange/change",//变美魔镜 模拟整形

  /**
   * 生命周期函数--监听小程序初始化
   */
  onLaunch(e) {
    let _this = this;
    // 小程序主动更新
    _this.updateManager();
    // 小程序启动场景
    _this.onStartupScene(e.query);
  },

  /**
   * 小程序启动场景
   */
  onStartupScene(query) {
    // 获取场景值
    let scene = this.getSceneData(query);
    // 记录推荐人id
    let refereeId = query.referee_id ? query.referee_id : scene.uid;
    // live
    if(refereeId === '' || refereeId === undefined){
      let scene_user = query.scene;
      if(scene_user) {
        var scene_user_arr = scene_user.split('%26');
        refereeId = scene_user_arr[3];
      }
    }
    refereeId > 0 && (this.saveRefereeId(refereeId));
    // 记录推广参数
    let source = query.source ? query.source : scene.source;
    source && wx.setStorageSync('source', source);
  },

  /**
   * 获取商城ID
   */
  getWxappId() {
    return siteinfo.uniacid || 10001;
  },

  /**
   * 记录推荐人id
   */
  saveRefereeId(refereeId) {
    let App = this;
    refereeId = parseInt(refereeId);
    if (refereeId <= 0 || refereeId == App.getUserId()) {
      return false;
    }
    wx.setStorageSync('referee_id', refereeId);
    return true;
  },

  /**
   * 获取场景值(scene)
   */
  getSceneData(query) {
    return query.scene ? util.scene_decode(query.scene) : {};
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow(options) {
    let App = this;
    try {
      const livePlayer = requirePlugin('live-player-plugin');
      if (options.scene == 1007 || options.scene == 1008 || options.scene == 1044) {
        livePlayer.getShareParams()
          .then(res => {
            // 直播页面的自定义参数
            let customParams = res.custom_params;
            //console.log('get custom params', customParams);
            // 记录推荐人ID
            if (customParams.hasOwnProperty('referee_id')) {
              App.saveRefereeId(customParams['referee_id']);
            }
          }).catch(err => {
            console.log('get share params', err)
          });
      }
    } catch (error) {

    }
  },

  /**
   * 执行用户登录
   */
  doLogin(delta,path) {
    // 保存当前页面
    let pages = getCurrentPages();
    if (pages.length) {
      let currentPage = pages[pages.length - 1];
      "pages/login/login" != currentPage.route &&
        wx.setStorageSync("currentPage", currentPage);
    }
    // 跳转授权页面
    wx.navigateTo({
      url: "/pages/login/login?delta=" + (delta || 1) + (typeof(path) !== 'undefined'? "&path=" + path : '')
    });
  },
  /**
   * 当前用户id
   */
  getUserId() {
    return wx.getStorageSync('user_id');
  },

  /**
   * 显示成功提示框
   */
  showSuccess(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      mask: true,
      duration: 1500,
      success() {
        callback && (setTimeout(() => {
          callback();
        }, 1500));
      }
    });
  },

  /**
   * 显示失败提示框
   */
  showError(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success(res) {
        callback && callback(res);
      }
    });
  },

  /**
   * get请求
   */
  _get(url, data, success, fail, complete, check_login) {
    wx.showNavigationBarLoading();
    let _this = this;
    // 构造请求参数
    data = data || {};
    data.wxapp_id = _this.getWxappId();

    // if (typeof check_login === 'undefined')
    //   check_login = true;

    // 构造get请求
    let request = () => {
      data.token = wx.getStorageSync('token');
      wx.request({
        url: _this.api_root + url,
        header: {
          'content-type': 'application/json'
        },
        data: data,
        success(res) {
          if (res.statusCode !== 200 || typeof res.data !== 'object') {
            console.log(res);
            _this.showError('网络请求出错');
            return false;
          }
          if (res.data.code === -1) {
            // 登录态失效, 重新登录
            wx.hideNavigationBarLoading();
            wx.hideLoading();
            _this.doLogin(2);
          } else if (res.data.code === 0) {
            wx.hideLoading();
            _this.showError(res.data.msg, (err) => {
              fail && fail(res, err);
            });
            return false;
          } else {
            success && success(res.data);
          }
        },
        fail(res) {
          wx.hideLoading();
          _this.showError(res.errMsg, (err) => {
            fail && fail(res, err);
          });
        },
        complete(res) {
          wx.hideNavigationBarLoading();
          complete && complete(res);
        },
      });
    };
    // 判断是否需要验证登录
    check_login ? _this.doLogin(request) : request();
  },

  /**
   * post提交
   */
  _post_form(url, data, success, fail, complete, isShowNavBarLoading) {
    let _this = this;

    isShowNavBarLoading || true;
    data.wxapp_id = _this.getWxappId();
    data.token = wx.getStorageSync('token');

    // 在当前页面显示导航条加载动画
    if (isShowNavBarLoading == true) {
      wx.showNavigationBarLoading();
    }
    wx.request({
      url: _this.api_root + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: data,
      success(res) {
        if (res.statusCode !== 200 || typeof res.data !== 'object') {
          _this.showError('网络请求出错');
          return false;
        }
        if (res.data.code === -1) {
          // 登录态失效, 重新登录
          wx.hideNavigationBarLoading();
          wx.hideLoading();
          _this.doLogin(1);
          return false;
        } else if (res.data.code === 0) {
          wx.hideLoading();
          _this.showError(res.data.msg, (err) => {
            fail && fail(res, err);
          });
          return false;
        }
        success && success(res.data);
      },
      fail(res) {
        wx.hideLoading();
        _this.showError(res.errMsg, (err) => {
          fail && fail(res, err);
        });
      },
      complete(res) {
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },

  /**
   * 验证是否存在user_info
   */
  validateUserInfo() {
    let user_info = wx.getStorageSync('user_info');
    return !!wx.getStorageSync('user_info');
  },

  /**
   * 小程序主动更新
   */
  updateManager() {
    if (!wx.canIUse('getUpdateManager')) {
      return false;
    }
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(res => {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
    });
    updateManager.onUpdateReady(() => {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，即将重启应用',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      });
    });
    updateManager.onUpdateFailed(() => {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    });
  },

  /**
   * 获取tabBar页面路径列表
   */
  getTabBarLinks() {
    return tabBarLinks;
  },

  /**
   * 跳转到指定页面
   * 支持tabBar页面
   */
  navigationTo(url) {
    if (!url || url.length == 0) {
      return false;
    }
    let tabBarLinks = this.getTabBarLinks();
    // 如果携带了参数
    var subUrl = url;
    if (url.indexOf('?') > -1) {
      subUrl = url.substr(0, url.indexOf('?'))
    }
    // tabBar页面
    if (tabBarLinks.indexOf(subUrl) > -1) {
      wx.switchTab({
        url: '/' + url
      });
    } else {
      // 普通页面
      wx.navigateTo({
        url: '/' + url
      });
    }
  },

  /**
   * 生成转发的url参数
   */
  getShareUrlParams(params) {
    let _this = this;
    !_this.checkIsLogin() && _this.doLogin(1);
    return util.urlEncode(Object.assign({
      referee_id: _this.getUserId()
    }, params));
  },

  /**
   * 发起微信支付
   */
  wxPayment(option) {
    let options = Object.assign({
      payment: {},
      success: () => { },
      fail: () => { },
      complete: () => { },
    }, option);
    wx.requestPayment({
      timeStamp: options.payment.timeStamp,
      nonceStr: options.payment.nonceStr,
      package: 'prepay_id=' + options.payment.prepay_id,
      signType: 'MD5',
      paySign: options.payment.paySign,
      success(res) {
        options.success(res);
      },
      fail(res) {
        options.fail(res);
      },
      complete(res) {
        options.complete(res);
      }
    });
  },

  /**
   * 验证登录
   */
  checkIsLogin() {
    return wx.getStorageSync('token') != '' && wx.getStorageSync('user_id') != '';
  },

  /**
   * 判断是否登录、手机号非空(sg添加)
   */
  checkUserPhone() {
    console.log(wx.getStorageSync('ACCESS_TOKEN'))
    let App = this,token = wx.getStorageSync('ACCESS_TOKEN'),phone = wx.getStorageSync('USER_INFO').phone;
    if(token){
      if(phone == '') {
        App._get('user/detail', {}, (result) => {
          result.data.userInfo.phone == '' && wx.navigateTo({url: '/pages/phone/index'})
        })
      }
    }else{
      wx.navigateTo({url: '/pages/login/login'})
    }
    return token && phone != '' ? true : false
  },

  /**
   * 授权登录 手机端 getUserProfile 获取用户所有信息
   */
  getUserProfile(e, callback) {
    let App = this;
    console.log(e)
    if (e.errMsg !== 'getUserProfile:ok') {
      return false;
    }
    wx.showLoading({
      title: "正在登录",
      mask: true
    });
    // 执行微信登录
    wx.login({
      success(res) {
        // 发送用户信息
        App._post_form('user/login', {
          code: res.code,
          user_info: e.rawData,
          encrypted_data: e.encryptedData,
          iv: e.iv,
          signature: e.signature,
          referee_id: wx.getStorageSync('referee_id')
        }, result => {
          // 记录token user_id
          console.log(result);
          wx.setStorageSync('token', result.data.token);
          wx.setStorageSync('user_id', result.data.user_id);
          wx.setStorageSync('ACCESS_TOKEN', result.data.user_openid);
          wx.setStorageSync('USER_INFO', result.data.user_info);
          wx.setStorageSync('appUserInfo', result.data.user_info);
          // 执行回调函数
          callback && callback();
        }, false, () => {
          wx.hideLoading();
        });
      }
    });
  },

  /**
   * 授权登录 PC端微信小程序暂时 默认使用 getUserInfo 方法获取获取用户信息
   */
  getUserInfo(e, callback) {
    let App = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
    wx.showLoading({
      title: "正在登录",
      mask: true
    });
    // 执行微信登录
    wx.login({
      success(res) {
        // 发送用户信息
        App._post_form('user/login', {
          code: res.code,
          user_info: e.detail.rawData,
          encrypted_data: e.detail.encryptedData,
          iv: e.detail.iv,
          signature: e.detail.signature,
          referee_id: wx.getStorageSync('referee_id')
        }, result => {
          // 记录token user_id
          wx.setStorageSync('token', result.data.token);
          wx.setStorageSync('user_id', result.data.user_id);
          wx.setStorageSync('ACCESS_TOKEN', result.data.user_openid);
          wx.setStorageSync('USER_INFO', result.data.user_info);
          wx.setStorageSync('appUserInfo', result.data.user_info);
          // 执行回调函数
          callback && callback();
        }, false, () => {
          wx.hideLoading();
        });
      }
    });
  },

});